package org.blogwu.sso.until.redisUntil.jdeis;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedisPool;

@Configuration
public class JedisClusterConfig {

	@Autowired
	private RedisProperties redisProperties;



	/*public JedisCluster getJedisCluster() {
		String[] serverArray = redisProperties.getClusterNodes().split(",");//获取服务器数组(这里要相信自己的输入，所以没有考虑空指针问题)
		System.out.println(redisProperties.toString());

		Set<HostAndPort> nodes = new HashSet<>();

		for (String ipPort : serverArray) {
			String[] ipPortPair = ipPort.split(":");
			nodes.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim())));
		}
		System.out.println("JedisClusterConfig.getJedisCluster");
		GenericObjectPoolConfig gg=new GenericObjectPoolConfig();
		gg.setMaxIdle(100);
		gg.setMinIdle(10);
		gg.setTestOnBorrow(true);
		System.out.println(redisProperties.getCommandTimeout());
		return new JedisCluster(nodes, redisProperties.getCommandTimeout(),gg);
	}
	 */


	@Bean 
	public JedisCluster getJedisCluster() {
		//解析地址
		String[] serverArray = redisProperties.getClusterNodes().split(",");//获取服务器数组(这里要相信自己的输入，所以没有考虑空指针问题)
		Set<HostAndPort> nodes = new HashSet<>();
		for (String ipPort : serverArray) {
			String[] ipPortPair = ipPort.split(":");
			nodes.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim())));
		}
		GenericObjectPoolConfig gg=new GenericObjectPoolConfig();
		gg.setMaxIdle(redisProperties.getMaxIdle());
		gg.setMinIdle(redisProperties.getMinIdle());
		gg.setTestOnBorrow(redisProperties.isTestOnBorrow());
		gg.setMaxWaitMillis(redisProperties.getmaxWaitMillis());
		gg.setMaxTotal(redisProperties.getMaxActive());
		return new JedisCluster(nodes, redisProperties.getCommandTimeout(),gg);
	}



	public ShardedJedisPool getShardedJedisPool() {
		String[] serverArray = redisProperties.getClusterNodes().split(",");//获取服务器数组(这里要相信自己的输入，所以没有考虑空指针问题)
		List<JedisShardInfo> shards=new ArrayList<JedisShardInfo>();
		for (String ipPort : serverArray) {
			String[] ipPortPair = ipPort.split(":");
			System.out.println(ipPortPair[0].trim()+"    "+Integer.valueOf(ipPortPair[1].trim())+"    "+Integer.valueOf(ipPortPair[2].trim()));
			shards.add(new JedisShardInfo(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim()),Integer.valueOf(ipPortPair[2].trim())));
		}		
		JedisPoolConfig gg=new JedisPoolConfig();
		gg.setMaxIdle(100);
		gg.setMinIdle(10);
		gg.setTestOnBorrow(true);
		gg.setMaxTotal(120);
		gg.setMaxWaitMillis(3000);

		ShardedJedisPool ppc= new ShardedJedisPool(gg,shards);
		return ppc;
	}


	/**
	 * 注意：
	 * 这里返回的JedisCluster是单例的，并且可以直接注入到其他类中去使用
	 * @return
	 */
}